﻿#ifndef SIMUFACTORY_H
#define SIMUFACTORY_H

#include <QDateTime>

#include "simuvalue.h"
#include "datasimucache.h"
#include "globalstatic.h"

class SimuFactory
{
public:
    static AbstractSimuValue* produceSimuValue(const ChannelDescription& channelDescription) {
       if (QString::compare(channelDescription.simuType, "const") == 0) {
           return new ConstSimuValue(channelDescription.initValue);
       } else if (QString::compare(channelDescription.simuType, "assign") == 0) {
           return new AssignSimuValue(DataSimuCache::getSimuValue(channelDescription.id));
       } else if (QString::compare(channelDescription.simuType, "increase") == 0) {
           return new IncreaseSimuValue(DataSimuCache::getSimuValue(channelDescription.id), channelDescription.dataType, channelDescription.increaseStep);
       } else if (QString::compare(channelDescription.simuType, "sin") == 0) {
           return generateSinValue();
       } else if (QString::compare(channelDescription.simuType, "random") == 0) {
           return new RandomSimuValue(channelDescription.minValue, channelDescription.maxValue);
       } else {
           return nullptr;
       }
    }

private:
    static AbstractSimuValue* generateSinValue() {
        if (!GlobalStatic::getSining())
            return new SinSimuValue(0);

        qint64 currentTimeStamp = QDateTime::currentMSecsSinceEpoch();
        qint64 sinDestTimeStamp = GlobalStatic::getSinDestTimeStamp();
        if (currentTimeStamp > sinDestTimeStamp)
            return new SinSimuValue(0);

        qint64 timeSpan = currentTimeStamp - GlobalStatic::getSinZeroTimeStamp();
        double sinT = GlobalStatic::getSinT();
        double sinA = GlobalStatic::getSinA();
        bool sinSquare = GlobalStatic::getSinSquare();

        return new SinSimuValue(timeSpan, sinT, sinA, sinSquare);
    }

};

#endif // SIMUFACTORY_H
